﻿Imports System.Data.SqlClient
Public Class frmQuiDinh
    Dim cnStr As String = "Data Source=.\SQLEXPRESS;Initial Catalog=QLKS;Integrated Security=True"
    Dim con As New SqlConnection(cnStr)

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
        Me.Close()

    End Sub

    Private Sub frmQuiDinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadcb()
        loadGV()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        Dim Sql As String = "update ThamSo set QuiDinh=@QuiDinh, MaTrangThai=@MaTrangThai where MaThamSo=@MaThamSo"
        Dim com As New SqlCommand(Sql, con)

        com.Parameters.Add("@QuiDinh", SqlDbType.NVarChar)
        com.Parameters("@QuiDinh").Value = DataGridView1.CurrentRow.Cells("QuiDinh").Value.ToString()

        com.Parameters.Add("@MaTrangThai", SqlDbType.Int)
        com.Parameters("@MaTrangThai").Value = DataGridView1.CurrentRow.Cells("CbTrangThai").Value

        com.Parameters.Add("@MaThamSo", SqlDbType.Int)
        com.Parameters("@MaThamSo").Value = DataGridView1.CurrentRow.Cells("MaThamSo").Value.ToString()

        com.Connection.Open()
        Dim i As Integer = com.ExecuteNonQuery()
        com.Connection.Close()
        If (i > 0) Then
            MessageBox.Show("Cập nhật thành công!")
            loadGV()
        Else
            MessageBox.Show("Xảy ra lỗi!")
        End If
    End Sub
    Private Sub loadGV()

        Dim sql As String = "select  QuiDinh,TrangThai,ThamSo.MaTrangThai as MaTrangThai,MaThamSo from ThamSo,TrangThai where ThamSo.MaTrangThai=TrangThai.MaTrangThai"
        Dim bs As New BindingSource()
        Dim da As New SqlDataAdapter(sql, con)
        Dim dt As New DataTable()
        da.Fill(dt)
        bs.DataSource = dt
        DataGridView1.DataSource = bs
      
    End Sub
    Private Sub loadcb()
        Dim Sql As String = "select * from TrangThai"
        Dim mycb As New DataGridViewComboBoxColumn

        Dim da As New SqlDataAdapter(Sql, con)
        Dim dt As New DataTable()
        da.Fill(dt)
        mycb.DataSource = dt
        mycb.Name = "CbTrangThai"
        mycb.DisplayMember = "TrangThai"
        'mycb.DataPropertyName = "MaTrangThai"
        mycb.ValueMember = "MaTrangThai"
        mycb.HeaderText = "Chọn tình trạng"
        mycb.FillWeight = 200
        Me.DataGridView1.Columns.Add(mycb)
        mycb.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    End Sub
    
    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click

        Dim Sql As String = "delete from ThamSo where MaThamSo=@MaThamSo"
        Dim com As New SqlCommand(Sql, con)


        com.Parameters.Add("@MaThamSo", SqlDbType.Int)
        com.Parameters("@MaThamSo").Value = DataGridView1.CurrentRow.Cells("MaThamSo").Value.ToString()

        com.Connection.Open()
        Dim i As Integer = com.ExecuteNonQuery()
        com.Connection.Close()
        If (i > 0) Then
            MessageBox.Show("Xóa thành công!")
            DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
            loadGV()
        Else
            MessageBox.Show("Xảy ra lỗi!")
        End If
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim Sql As String = "insert into ThamSo(QuiDinh,MaTrangThai) values(@QuiDinh,@MaTrangThai)"
        Dim com As New SqlCommand(Sql, con)

        com.Parameters.Add("@QuiDinh", SqlDbType.NVarChar)
        com.Parameters("@QuiDinh").Value = DataGridView1.CurrentRow.Cells("QuiDinh").Value.ToString()

        com.Parameters.Add("@MaTrangThai", SqlDbType.Int)
        com.Parameters("@MaTrangThai").Value = DataGridView1.CurrentRow.Cells("CbTrangThai").Value


        com.Connection.Open()
        Dim i As Integer = com.ExecuteNonQuery()
        com.Connection.Close()
        If (i > 0) Then
            MessageBox.Show("Thêm thành công!")
            loadGV()
        Else
            MessageBox.Show("Xảy ra lỗi!")
        End If
    End Sub
End Class